<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="memberId">
        <el-input
            v-model="queryParams.skuName"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderSn">
        <el-input
            v-model="queryParams.orderSn"
            placeholder="请输入订单号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品sku编号" prop="skuId">
        <el-input
            v-model="queryParams.skuId"
            placeholder="请输入商品sku编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['clientorder:clientorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clientorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="订单号" align="center" prop="orderSn" />
          <el-table-column label="商品名称" align="center" prop="skuName" />
<!--          <el-table-column label="商品图片" align="center" prop="skuPic" />-->
          <el-table-column label="商品图片" align="center">
            <template #default="{ row }">
<!--              <el-input-number v-model="row.detailsCount" @change="handleCountChange(row)" :min="1" :max="100" />-->
              <img :src="row.skuPic" alt="图片损坏" style="width: 100px;height: 100px">
            </template>
          </el-table-column>
          <el-table-column label="商品价格" align="center" prop="skuPrice" />
          <el-table-column label="商品数量" align="center" prop="skuQuantity" />
<!--          <el-table-column label="使用的优惠券" align="center" prop="couponId" />-->
          <el-table-column label="用户名" align="center" prop="memberUsername" />
          <el-table-column label="订单总额" align="center" prop="totalAmount" />
<!--          <el-table-column label="运费金额" align="center" prop="freightAmount" />-->
<!--          <el-table-column label="支付方式【1-&gt;支付宝；2-&gt;微信；3-&gt;银联； 4-&gt;货到付款；】" align="center" prop="payType" />-->
<!--          <el-table-column label="订单来源[0-&gt;PC订单；1-&gt;app订单]" align="center" prop="sourceType" />-->
          <el-table-column label="订单状态【0-&gt;待付款；1-&gt;待发货；2-&gt;已发货；3-&gt;已完成；4-&gt;已关闭；5-&gt;无效订单】" align="center" prop="status" />
<!--          <el-table-column label="发票类型[0-&gt;不开发票；1-&gt;电子发票；2-&gt;纸质发票]" align="center" prop="billType" />-->
          <el-table-column label="收货人姓名" align="center" prop="receiverName" />
          <el-table-column label="收货人电话" align="center" prop="receiverPhone" />
          <el-table-column label="收货人邮编" align="center" prop="receiverPostCode" />
<!--          <el-table-column label="省份/直辖市" align="center" prop="receiverProvince" />-->
<!--          <el-table-column label="城市" align="center" prop="receiverCity" />-->
<!--          <el-table-column label="区" align="center" prop="receiverRegion" />-->
          <el-table-column label="详细地址" align="center" prop="receiverDetailAddress" />
<!--          <el-table-column label="订单备注" align="center" prop="note" />-->
<!--          <el-table-column label="删除状态【0-&gt;未删除；1-&gt;已删除】" align="center" prop="deleteStatus"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">查看详情</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['clientorder:clientorder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Clientorder">
  import { listClientorder, getClientorder, delClientorder, addClientorder, updateClientorder } from "@/api/clientorder/clientorder";

  const { proxy } = getCurrentInstance();
  import { useRouter } from 'vue-router'
  const router=useRouter();
  const clientorderList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderSn: null,
      couponId: null,
      skuName:null,
      skuPic:null,
      skuPrice:null,
      skuQuantity:null,
      memberUsername: null,
      totalAmount: null,
      freightAmount: null,
      promotionAmount: null,
      integrationAmount: null,
      couponAmount: null,
      discountAmount: null,
      payType: null,
      sourceType: null,
      status: null,
      billType: null,
      receiverName: null,
      receiverPhone: null,
      receiverPostCode: null,
      receiverProvince: null,
      receiverCity: null,
      receiverRegion: null,
      receiverDetailAddress: null,
      note: null,
      deleteStatus: null,
    },
    rules: {
    }
  });

  const { queryParams, form, rules } = toRefs(data);

  /** 查询订单列表 */
  function getList() {
    loading.value = true;
    listClientorder(queryParams.value).then(response => {
      clientorderList.value = response.data.rows;
      total.value = response.data.total;
      loading.value = false;
    });
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
     id: null,
     memberId: null,
     orderSn: null,
     couponId: null,
     createTime: null,
     memberUsername: null,
     totalAmount: null,
     payAmount: null,
     freightAmount: null,
     promotionAmount: null,
     integrationAmount: null,
     couponAmount: null,
     discountAmount: null,
     payType: null,
     sourceType: null,
     status: null,
     deliveryCompany: null,
     deliverySn: null,
     autoConfirmDay: null,
     integration: null,
     growth: null,
     billType: null,
     billHeader: null,
     billContent: null,
     billReceiverPhone: null,
     billReceiverEmail: null,
     receiverName: null,
     receiverPhone: null,
     receiverPostCode: null,
     receiverProvince: null,
     receiverCity: null,
     receiverRegion: null,
     receiverDetailAddress: null,
     note: null,
     confirmStatus: null,
     deleteStatus: null,
     useIntegration: null,
     paymentTime: null,
     deliveryTime: null,
     receiveTime: null,
     commentTime: null,
     modifyTime: null
    };
    proxy.resetForm("clientorderRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加订单";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    router.push({path: "/clientorder/clientOrderDetails", query:{orderSn:row.orderSn}})
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["clientorderRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updateClientorder(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addClientorder(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除订单编号为"' + _ids + '"的数据项？').then(function() {
      return delClientorder(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('clientorder/clientorder/export', {
      ...queryParams.value
    }, `clientorder_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
